home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue72 / misc / nielsen / sql / class.table_functions next >
Encoding:
Text File  |  2002-08-14  |  21.5 KB  |  641 lines

  1. drop sequence class_sequence;
  2. create sequence class_sequence;
  3. drop sequence class_sequence_backup;
  4. create sequence class_sequence_backup;
  5. drop table class;
  6. create table class (
  7. class_id int4 NOT NULL PRIMARY KEY DEFAULT nextval('class_sequence'),
  8.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  9.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  10.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  11. time text not null default '' ,
  12. title text not null default '' ,
  13. description text not null default '' ,
  14. users_id int4 not null default 0  REFERENCES users
  15.        ON DELETE NO ACTION
  16.        ON UPDATE CASCADE 
  17. );drop table class_backup;
  18. create table class_backup (
  19. backup_id int4 NOT NULL UNIQUE DEFAULT nextval('class_sequence_backup'), 
  20.     class_id int4 NOT NULL DEFAULT 0,
  21.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  22.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  23.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  24.     time text not null default '',
  25. title text not null default '',
  26. description text not null default '',
  27. users_id int4 not null default 0, error_code text NOT NULL DEFAULT ''
  28. );
  29. drop view class_active;
  30. create view class_active as select * from class
  31.         where active = 1;
  32. drop view class_deleted;
  33. create view class_deleted as select * from class
  34.         where active = 0;
  35. drop view class_backup_ids;
  36. create view class_backup_ids as 
  37.            select distinct class_id from class_backup;
  38. drop view class_purged;
  39. create view class_purged as
  40.    select * from class_backup where oid = ANY (
  41.      select max(oid) from class_backup where class_id = ANY
  42.         (
  43.         select distinct class_id from class_backup
  44.           where class_backup.error_code = 'purge'
  45.            and NOT class_id = ANY (select class_id from class)
  46.         )
  47.         group by class_id
  48.      )
  49.     ;
  50. ---              Generic Functions for Perl/Postgresql version 1.0
  51.  
  52. ---                       Copyright 2001, Mark Nielsen
  53. ---                            All rights reserved.
  54. ---    This Copyright notice was copied and modified from the Perl 
  55. ---    Copyright notice. 
  56. ---    This program is free software; you can redistribute it and/or modify
  57. ---    it under the terms of either:
  58.  
  59. ---        a) the GNU General Public License as published by the Free
  60. ---        Software Foundation; either version 1, or (at your option) any
  61. ---        later version, or
  62.  
  63. ---        b) the "Artistic License" which comes with this Kit.
  64.  
  65. ---    This program is distributed in the hope that it will be useful,
  66. ---    but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. ---    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
  68. ---    the GNU General Public License or the Artistic License for more details.
  69.  
  70. ---    You should have received a copy of the Artistic License with this
  71. ---    Kit, in the file named "Artistic".  If not, I'll be glad to provide one.
  72.  
  73. ---    You should also have received a copy of the GNU General Public License
  74. ---   along with this program in the file named "Copying". If not, write to the 
  75. ---   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
  76. ---    02111-1307, USA or visit their web page on the internet at
  77. ---    http://www.gnu.org/copyleft/gpl.html.
  78.  
  79. -- create a method to unpurge just one item.  
  80. -- create a method to purge one item. 
  81. --  \i /tmp/Test/sample/class.table
  82. ---------------------------------------------------------------------
  83.  
  84. drop function sql_class_insert ();
  85. CREATE FUNCTION sql_class_insert () RETURNS int4 AS '
  86. DECLARE
  87.     record1 record;  oid1 int4; id int4 :=0; record_backup RECORD;
  88. BEGIN
  89.    insert into class (date_updated, date_created, active)
  90.         values (CURRENT_TIMESTAMP,CURRENT_TIMESTAMP, 1);
  91.      -- Get the unique oid of the row just inserted. 
  92.    GET DIAGNOSTICS oid1 = RESULT_OID;
  93.      -- Get the class id. 
  94.    FOR record1 IN SELECT class_id FROM class where oid = oid1
  95.       LOOP
  96.       id := record1.class_id;
  97.    END LOOP;
  98.    
  99.      -- If id is NULL, insert failed or something is wrong.
  100.    IF id is NULL THEN return (-1); END IF;
  101.      -- It should also be greater than 0, otherwise something is wrong.
  102.    IF id < 1 THEN return (-2); END IF;
  103.  
  104.       -- Now backup the data. 
  105.     FOR record_backup IN SELECT * FROM class where class_id = id
  106.        LOOP
  107.        insert into class_backup (class_id, date_updated, date_created, 
  108.            active, error_code) 
  109.          values (id, record_backup.date_updated, record_backup.date_created,
  110.             record_backup.active, ''insert'');
  111.     END LOOP;
  112.  
  113.      -- Everything has passed, return id as class_id.
  114.    return (id);
  115. END;
  116. ' LANGUAGE 'plpgsql';
  117. ---------------------------------------------------------------------
  118.  
  119. drop function sql_class_delete (int4);
  120. CREATE FUNCTION sql_class_delete (int4) RETURNS int2 AS '
  121. DECLARE
  122.     id int4 := 0;
  123.     id_exists int4 := 0;
  124.     record1 RECORD; 
  125.     record_backup RECORD;
  126.     return_int4 int4 :=0;
  127.  
  128. BEGIN
  129.      -- If the id is not greater than 0, return error.
  130.    id := clean_numeric($1);
  131.    IF id < 1 THEN return -1; END IF;
  132.  
  133.      -- If we find the id, set active = 0. 
  134.    FOR record1 IN SELECT class_id FROM class 
  135.           where class_id = id
  136.       LOOP
  137.       update class set active=0, date_updated = CURRENT_TIMESTAMP
  138.            where class_id = id;  
  139.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  140.       id_exists := 1;
  141.    END LOOP;
  142.       
  143.      -- If we did not find the id, abort and return -2.  
  144.    IF id_exists = 0 THEN return (-2); END IF;
  145.  
  146.    FOR record_backup IN SELECT * FROM class where class_id = id
  147.       LOOP
  148.       insert into class_backup (class_id, date_updated, date_created,
  149.           active , time, title, description, users_id ,error_code)
  150.            values (record_backup.class_id, record_backup.date_updated, 
  151.              record_backup.date_updated, record_backup.active
  152.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''delete''
  153.       );
  154.    END LOOP;
  155.  
  156.      -- If id_exists == 0, Return error.
  157.      -- It means it never existed. 
  158.    IF id_exists = 0 THEN return (-1); END IF;
  159.  
  160.      -- We got this far, it must be true, return ROW_COUNT.   
  161.    return (return_int4);
  162. END;
  163. ' LANGUAGE 'plpgsql';
  164.  
  165. ---------------------------------------------------------------------
  166.  
  167. drop function sql_class_undelete (int4);
  168. CREATE FUNCTION sql_class_undelete (int4) RETURNS int2 AS '
  169. DECLARE
  170.     id int4 := 0;
  171.     id_exists int4 := 0;
  172.     record1 RECORD; 
  173.     record_backup RECORD;
  174.     return_int4 int4 :=0;
  175.  
  176. BEGIN
  177.      -- If the id is not greater than 0, return error.
  178.    id := clean_numeric($1);
  179.    IF id < 1 THEN return -1; END IF;
  180.  
  181.      -- If we find the id, set active = 1. 
  182.    FOR record1 IN SELECT class_id FROM class 
  183.           where class_id = id
  184.       LOOP
  185.       update class set active=1, date_updated = CURRENT_TIMESTAMP
  186.            where class_id = id;  
  187.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  188.       id_exists := 1;
  189.    END LOOP;
  190.       
  191.      -- If we did not find the id, abort and return -2.  
  192.    IF id_exists = 0 THEN return (-2); END IF;
  193.  
  194.    FOR record_backup IN SELECT * FROM class where class_id = id
  195.       LOOP
  196.       insert into class_backup (class_id, date_updated, date_created,
  197.           active , time, title, description, users_id ,error_code)
  198.            values (record_backup.class_id, record_backup.date_updated, 
  199.              record_backup.date_updated, record_backup.active
  200.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''undelete''
  201.       );
  202.    END LOOP;
  203.  
  204.      -- If id_exists == 0, Return error.
  205.      -- It means it never existed. 
  206.    IF id_exists = 0 THEN return (-1); END IF;
  207.  
  208.      -- We got this far, it must be true, return ROW_COUNT.   
  209.    return (return_int4);
  210. END;
  211. ' LANGUAGE 'plpgsql';
  212.  
  213. ---------------------------------------------------------------------
  214. drop function sql_class_update (int4 , text, text, text, int4);
  215. CREATE FUNCTION sql_class_update  (int4 , text, text, text, int4) 
  216.   RETURNS int2 AS '
  217. DECLARE
  218.     id int4 := 0;
  219.     id_exists int4 := 0;
  220.     record_update RECORD; record_backup RECORD;
  221.     return_int4 int4 :=0;
  222.               var_2 text;
  223.           var_3 text;
  224.           var_4 text;
  225.           var_5 int4;
  226.  
  227. BEGIN
  228.              var_2 := clean_text($2);
  229.          var_3 := clean_text($3);
  230.          var_4 := clean_text($4);
  231.          var_5 := clean_numeric($5);
  232.  
  233.      -- If the id is not greater than 0, return error.
  234.    id := clean_numeric($1);
  235.    IF id < 1 THEN return -1; END IF;
  236.  
  237.    FOR record_update IN SELECT class_id FROM class
  238.          where class_id = id
  239.       LOOP
  240.       id_exists := 1;
  241.    END LOOP;
  242.  
  243.    IF id_exists = 0 THEN return (-2); END IF;
  244.  
  245.    update class set date_updated = CURRENT_TIMESTAMP
  246.       , time = var_2, title = var_3, description = var_4, users_id = var_5 
  247.         where class_id = id;
  248.    GET DIAGNOSTICS return_int4 = ROW_COUNT;
  249.  
  250.    FOR record_backup IN SELECT * FROM class where class_id = id
  251.       LOOP
  252.      insert into class_backup (class_id,
  253.          date_updated, date_created, active
  254.          , time, title, description, users_id, error_code)
  255.        values (record_update.class_id, record_backup.date_updated,
  256.          record_backup.date_updated, record_backup.active
  257.          , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id, ''update''
  258.       );
  259.    END LOOP;
  260.  
  261.      -- We got this far, it must be true, return ROW_COUNT.   
  262.    return (return_int4);
  263. END;
  264. ' LANGUAGE 'plpgsql';
  265. ---------------------------------------------------------------------
  266.  
  267. drop function sql_class_copy (int4);
  268. CREATE FUNCTION sql_class_copy (int4) 
  269.   RETURNS int2 AS '
  270. DECLARE
  271.     id int4 := 0;
  272.     id_exists int4 := 0;
  273.     record1 RECORD; record2 RECORD; record3 RECORD;    
  274.     return_int4 int4 := 0;
  275.     id_new int4 := 0;
  276.     class_new int4 :=0;
  277. BEGIN
  278.      -- If the id is not greater than 0, return error.
  279.    id := clean_numeric($1);
  280.    IF id < 1 THEN return -1; END IF;
  281.  
  282.    FOR record1 IN SELECT class_id FROM class where class_id = id
  283.       LOOP
  284.       id_exists := 1;
  285.    END LOOP;
  286.    IF id_exists = 0 THEN return (-2); END IF;
  287.  
  288.      --- Get the new id
  289.    FOR record1 IN SELECT sql_class_insert() as class_insert
  290.       LOOP
  291.       class_new := record1.class_insert;
  292.    END LOOP;
  293.      -- If the class_new is not greater than 0, return error.
  294.    IF class_new < 1 THEN return -3; END IF;
  295.  
  296.    FOR record2 IN SELECT * FROM class where class_id = id
  297.       LOOP
  298.  
  299.      FOR record1 IN SELECT sql_class_update(class_new , clean_text(record2.time), clean_text(record2.title), clean_text(record2.description), clean_text(record2.users_id))
  300.         as class_insert
  301.       LOOP
  302.         -- execute some arbitrary command just to get it to pass. 
  303.       id_exists := 1;
  304.      END LOOP;
  305.    END LOOP;
  306.  
  307.      -- We got this far, it must be true, return new id.   
  308.    return (class_new);
  309. END;
  310. ' LANGUAGE 'plpgsql';
  311.  
  312. ------------------------------------------------------------------
  313. drop function sql_class_purge ();
  314. CREATE FUNCTION sql_class_purge () RETURNS int4 AS '
  315. DECLARE
  316.     record_backup RECORD; oid1 int4 := 0;
  317.     return_int4 int4 :=0;
  318.     deleted int4 := 0;
  319.     delete_count int4 :=0;
  320.     delete_id int4;
  321.  
  322. BEGIN 
  323.  
  324.      -- Now delete one by one. 
  325.    FOR record_backup IN SELECT * FROM class where active = 0
  326.       LOOP
  327.          -- Record the id we want to delete. 
  328.       delete_id = record_backup.class_id;
  329.  
  330.       insert into class_backup (class_id, date_updated, date_created,
  331.           active , time, title, description, users_id ,error_code)
  332.            values (record_backup.class_id, record_backup.date_updated, 
  333.              record_backup.date_updated, record_backup.active
  334.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''purge''
  335.           );
  336.  
  337.         -- Get the unique oid of the row just inserted. 
  338.       GET DIAGNOSTICS oid1 = RESULT_OID;
  339.  
  340.         -- If oid1 less than 1, return -1
  341.       IF oid1 < 1 THEN return (-2); END IF;
  342.         -- Now delete this from the main table.   
  343.       delete from class where class_id = delete_id;
  344.  
  345.         -- Get row count of row just deleted, should be 1. 
  346.       GET DIAGNOSTICS deleted = ROW_COUNT;
  347.         -- If deleted less than 1, return -3
  348.       IF deleted < 1 THEN return (-3); END IF;
  349.       delete_count := delete_count + 1;
  350.  
  351.     END LOOP;
  352.  
  353.      -- We got this far, it must be true, return the number of ones we had.  
  354.    return (delete_count);
  355. END;
  356. ' LANGUAGE 'plpgsql';
  357.  
  358. ------------------------------------------------------------------
  359. drop function sql_class_purgeone (int4);
  360. CREATE FUNCTION sql_class_purgeone (int4) RETURNS int4 AS '
  361. DECLARE
  362.     record_backup RECORD; oid1 int4 := 0;
  363.     record1 RECORD;
  364.     return_int4 int4 :=0;
  365.     deleted int4 := 0;
  366.     delete_count int4 :=0;
  367.     delete_id int4;
  368.     purged_no int4 := 0;
  369.  
  370. BEGIN
  371.  
  372.     delete_id := $1;
  373.         -- If purged_id less than 1, return -4
  374.     IF delete_id < 1 THEN return (-4); END IF;
  375.  
  376.    FOR record1 IN SELECT * FROM class 
  377.       where active = 0 and class_id = delete_id 
  378.       LOOP
  379.       purged_no := purged_no + 1;
  380.    END LOOP;
  381.  
  382.         -- If purged_no less than 1, return -1
  383.    IF purged_no < 1 THEN return (-1); END IF;
  384.  
  385.      -- Now delete one by one.
  386.    FOR record_backup IN SELECT * FROM class where class_id = delete_id
  387.       LOOP
  388.  
  389.       insert into class_backup (class_id, date_updated, date_created,
  390.           active , time, title, description, users_id ,error_code)
  391.            values (record_backup.class_id, record_backup.date_updated,
  392.              record_backup.date_updated, record_backup.active
  393.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''purgeone''
  394.           );
  395.  
  396.         -- Get the unique oid of the row just inserted.
  397.       GET DIAGNOSTICS oid1 = RESULT_OID;
  398.  
  399.         -- If oid1 less than 1, return -2
  400.       IF oid1 < 1 THEN return (-2); END IF;
  401.         -- Now delete this from the main table.
  402.       delete from class where class_id = delete_id;
  403.  
  404.         -- Get row count of row just deleted, should be 1.
  405.       GET DIAGNOSTICS deleted = ROW_COUNT;
  406.         -- If deleted less than 1, return -3
  407.       IF deleted < 1 THEN return (-3); END IF;
  408.       delete_count := delete_count + 1;
  409.  
  410.     END LOOP;
  411.  
  412.      -- We got this far, it must be true, return the number of ones we had.
  413.    return (delete_count);
  414. END;
  415. ' LANGUAGE 'plpgsql';
  416.  
  417. ------------------------------------------------------------------------
  418. drop function sql_class_unpurge ();
  419. CREATE FUNCTION sql_class_unpurge () RETURNS int2 AS '
  420. DECLARE
  421.     record1 RECORD;
  422.     record2 RECORD; 
  423.     record_backup RECORD;
  424.     purged_id int4 := 0;
  425.     purge_count int4 :=0;
  426.     timestamp1 timestamp;
  427.     purged_no int4 := 0;
  428.     oid1 int4 := 0;
  429.     oid_found int4 := 0;
  430.     highest_oid int4 := 0;
  431.  
  432. BEGIN
  433.  
  434.      -- Now get the unique ids that were purged. 
  435.    FOR record1 IN select distinct class_id from class_backup 
  436.        where class_backup.error_code = ''purge''
  437.           and NOT class_id = ANY (select class_id from class)
  438.       LOOP
  439.  
  440.       purged_id := record1.class_id;
  441.       timestamp1 := CURRENT_TIMESTAMP;
  442.       purged_no := purged_no + 1;
  443.       oid_found := 0;
  444.       highest_oid := 0;
  445.  
  446.         -- Now we have the unique id, find its latest date. 
  447.  
  448.       FOR record2 IN select max(oid) from class_backup 
  449.           where class_id = purged_id and error_code = ''purge''
  450.         LOOP 
  451.           -- record we got the date and also record the highest date.
  452.         oid_found := 1; 
  453.         highest_oid := record2.max;
  454.       END LOOP;
  455.  
  456.          -- If the oid_found is 0, return error. 
  457.       IF oid_found = 0 THEN return (-3); END IF;
  458.  
  459.         -- Now we have the latest date, get the values and insert them. 
  460.       FOR record_backup IN select * from class_backup 
  461.           where oid = highest_oid
  462.         LOOP 
  463.  
  464.       insert into class_backup (class_id, date_updated, date_created,
  465.           active , time, title, description, users_id ,error_code)
  466.            values (purged_id, record_backup.date_updated, 
  467.              timestamp1, record_backup.active
  468.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''unpurge''
  469.           );
  470.  
  471.         -- Get the unique oid of the row just inserted. 
  472.       GET DIAGNOSTICS oid1 = RESULT_OID;
  473.         -- If oid1 less than 1, return -1
  474.       IF oid1 < 1 THEN return (-1); END IF;
  475.  
  476.       insert into class (class_id, date_updated, date_created,
  477.           active , time, title, description, users_id)
  478.            values (purged_id, timestamp1,
  479.              timestamp1, record_backup.active
  480.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id );
  481.         -- Get the unique oid of the row just inserted.
  482.       GET DIAGNOSTICS oid1 = RESULT_OID;
  483.         -- If oid1 less than 1, return -2
  484.       IF oid1 < 1 THEN return (-2); END IF;
  485.  
  486.       END LOOP;
  487.  
  488.    END LOOP;
  489.  
  490.      -- We got this far, it must be true, return how many were affected.  
  491.    return (purged_no);
  492. END;
  493. ' LANGUAGE 'plpgsql';
  494.  
  495. ---------------------------------------------------------------------
  496. drop function sql_class_unpurgeone (int4);
  497. CREATE FUNCTION sql_class_unpurgeone (int4) RETURNS int2 AS '
  498. DECLARE
  499.     record_id int4;
  500.     record1 RECORD;
  501.     record2 RECORD;
  502.     record_backup RECORD;
  503.     return_int4 int4 :=0;
  504.     purged_id int4 := 0;
  505.     purge_count int4 :=0;
  506.     timestamp1 timestamp;
  507.     purged_no int4 := 0;
  508.     oid1 int4 := 0;
  509.     oid_found int4 := 0;
  510.     highest_oid int4 := 0;
  511.  
  512. BEGIN
  513.  
  514.       purged_id := $1;
  515.         -- If purged_id less than 1, return -1
  516.       IF purged_id < 1 THEN return (-1); END IF;
  517.         --- Get the current timestamp.
  518.       timestamp1 := CURRENT_TIMESTAMP;
  519.  
  520.    FOR record1 IN select distinct class_id from class_backup
  521.        where class_backup.error_code = ''purge''
  522.           and NOT class_id = ANY (select class_id from class)
  523.           and class_id = purged_id
  524.       LOOP
  525.       purged_no := purged_no + 1;
  526.  
  527.    END LOOP;
  528.  
  529.         -- If purged_no less than 1, return -1
  530.    IF purged_no < 1 THEN return (-3); END IF;
  531.  
  532.         -- Now find the highest oid.  
  533.    FOR record2 IN select max(oid) from class_backup
  534.           where class_id = purged_id and error_code = ''purge''
  535.         LOOP
  536.           -- record we got the date and also record the highest date.
  537.         oid_found := 1;
  538.         highest_oid := record2.max;
  539.     END LOOP;
  540.  
  541.          -- If the oid_found is 0, return error.
  542.     IF oid_found = 0 THEN return (-4); END IF;
  543.  
  544.         -- Now get the data and restore it. 
  545.     FOR record_backup IN select * from class_backup 
  546.           where oid  = highest_oid
  547.         LOOP 
  548.         -- Insert into backup that it was unpurged. 
  549.       insert into class_backup (class_id, date_updated, date_created,
  550.           active , time, title, description, users_id ,error_code)
  551.            values (purged_id, timestamp1, 
  552.              record_backup.date_created, record_backup.active
  553.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id , ''unpurgeone''
  554.           );
  555.  
  556.         -- Get the unique oid of the row just inserted. 
  557.       GET DIAGNOSTICS oid1 = RESULT_OID;
  558.         -- If oid1 less than 1, return -1
  559.       IF oid1 < 1 THEN return (-1); END IF;
  560.         -- Insert into live table. 
  561.       insert into class (class_id, date_updated, date_created,
  562.           active , time, title, description, users_id)
  563.            values (record_backup.class_id, timestamp1,
  564.              record_backup.date_updated, record_backup.active
  565.              , record_backup.time, record_backup.title, record_backup.description, record_backup.users_id );
  566.         -- Get the unique oid of the row just inserted.
  567.       GET DIAGNOSTICS oid1 = RESULT_OID;
  568.         -- If oid1 less than 1, return -2
  569.       IF oid1 < 1 THEN return (-2); END IF;
  570.  
  571.       END LOOP;
  572.  
  573.      -- We got this far, it must be true, return how many were affected (1).  
  574.    return (purged_no);
  575. END;
  576. ' LANGUAGE 'plpgsql';
  577.  
  578. insert into class (class_id, date_updated, date_created, active)
  579.     values (0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
  580. insert into class_backup (backup_id, class_id, 
  581.      date_updated, date_created, active, error_code)
  582.     values (0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0, 'table creation');
  583.  
  584.  
  585.  
  586.  
  587. drop function clean_text (text);
  588. CREATE FUNCTION  clean_text (text) RETURNS text AS '
  589.   my $Text = shift;
  590.     # Get rid of whitespace in front. 
  591.   $Text =~ s/^\\s+//;
  592.     # Get rid of whitespace at end. 
  593.   $Text =~ s/\\s+$//;
  594.     # Get rid of anything not text.
  595.   $Text =~ s/[^ a-z0-9\\/\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\=\\+\\\\\\|\[\\{\\]\\}\\;\\:\\''\\"\\,\\<\\.\\>\\?\\t\\n]//gi;
  596.     # Replace all multiple whitespace with one space. 
  597.   $Text =~ s/\\s+/ /g;
  598.   return $Text;
  599. ' LANGUAGE 'plperl';
  600.  -- Just do show you what this function cleans up. 
  601. select clean_text ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  602.  
  603. drop function clean_alpha (text);
  604. CREATE FUNCTION  clean_alpha (text) RETURNS text AS '
  605.   my $Text = shift;
  606.   $Text =~ s/[^a-z0-9_]//gi;
  607.   return $Text;
  608. ' LANGUAGE 'plperl';
  609.  -- Just do show you what this function cleans up. 
  610. select clean_alpha ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  611.  
  612. drop function clean_numeric (text);
  613. CREATE FUNCTION  clean_numeric (text) RETURNS int4 AS '
  614.   my $Text = shift;
  615.   $Text =~ s/[^0-9]//gi;
  616.   return $Text;
  617. ' LANGUAGE 'plperl';
  618.  -- Just do show you what this function cleans up.
  619. select clean_numeric ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  620.  
  621. drop function clean_numeric (int4);
  622. CREATE FUNCTION  clean_numeric (int4) RETURNS int4 AS '
  623.   my $Text = shift;
  624.   $Text =~ s/[^0-9]//gi;
  625.   return $Text;
  626. ' LANGUAGE 'plperl';
  627.  -- Just do show you what this function cleans up.
  628. select clean_numeric (11111);
  629.  
  630.  
  631.  
  632. select sql_class_insert();
  633. select sql_class_update(1,'2 hours', 'Introduction to Linux Part 1','This is a description of this class. Not much here so far.', 1);
  634.  
  635. select sql_class_insert();
  636. select sql_class_update(2,'3 hours', 'Introduction to Linux Part 2','This is a description of this class. Not much here so far.', 2);
  637.  
  638.  
  639.  
  640. vacuum;
  641.